package ru.surfstudio.graduate.handler;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import ru.surfstudio.graduate.R;
import ru.surfstudio.graduate.activity.AbstractRssActivity;
import ru.surfstudio.graduate.bean.ChannelManager;
import ru.surfstudio.graduate.data.Channel;
import ru.surfstudio.graduate.data.Feed;
import ru.surfstudio.graduate.service.HttpService;
import ru.surfstudio.graduate.task.FeedSearchTask;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class FeedsAddHandler extends Handler {

	private static final String TAG = "FeedsAddHandler";
	
	private AbstractRssActivity activity;
	
	public FeedsAddHandler(AbstractRssActivity activity) {
		super();
		attach(activity);
	}
	
	@Override
	public void handleMessage(Message msg) {
		Bundle data = msg.getData();
		
		if (activity != null && data != null) {
			int status = data.getInt(HttpService.MARK_STATUS);
			
			switch (status) {
				case HttpService.STATUS_RUNNING:
					Log.d(TAG, "STATUS_RUNNING");
					activity.showDialog(AbstractRssActivity.DIALOG_WAIT);
					break;
	
				case HttpService.STATUS_FINISHED:
					Log.d(TAG, "STATUS_FINISHED");
					
					new AddFeedsTask().execute(
							((FeedSearchTask) data.getSerializable(HttpService.MARK_TASK))
									.getSelectedChannel(),
							data.getSerializable(HttpService.MARK_RESPONSE));
					break;
					
				case HttpService.STATUS_ERROR:
					Log.d(TAG, "STATUS_ERROR");
					activity.dismissDialog(AbstractRssActivity.DIALOG_WAIT);
					Toast.makeText(activity, R.string.channel_result_load_error, Toast.LENGTH_LONG)
						.show();
					Log.w(TAG, activity.getString(R.string.channel_result_load_error));
					break;
			}
		}
	}
	
	public void attach(AbstractRssActivity activity) {
		this.activity = activity;
	}
	
	public void detach() {
		activity = null;
	}
	
	private class AddFeedsTask extends AsyncTask<Serializable, Void, Channel> {

		@Override
		protected Channel doInBackground(Serializable... params) {
			try {
				Channel channel = (Channel) params[0];
				List<Feed> feedList = (List<Feed>) params[1];
				if (channel == null || feedList == null || activity == null)
					return null;
				
				ChannelManager channelManager = new ChannelManager(activity.getHelper());
				return channelManager.createChannelWithFeeds(channel, feedList);
				
			} catch (SQLException e) {
				Log.e(TAG, e.getMessage(), e);
			} catch (Exception e) {
				Log.e(TAG, e.getMessage(), e);
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(Channel channel) {
			if (activity != null) {
				activity.dismissDialog(AbstractRssActivity.DIALOG_WAIT);
				if (channel == null) {
					Toast.makeText(activity, R.string.database_error, Toast.LENGTH_LONG).show();
					Log.w(TAG, activity.getString(R.string.database_error));
				}
				else {
					activity.onPostFeedsUpdate(channel);
				}
			}
		}
		
	}
	
}
